// pages/user/index.js
import { areaList } from '@vant/area-data';

Page({
    data: {
        userInfo: {},
        // 被收藏的商品的数量
        collectNums: 0,
        hasLogin: false,
        areaList,
        name: "",
        detailAddress: "",
        phoneNum: "",
        area: "",
        show: false,
        select: "",
        town: "",
        province: "",
        city: "",

    },
    cancel() {
        this.setData({
            show: false
        })
    },
    confirm(e) {
        this.setData({
            province: e.detail.values[0].code,
            city: e.detail.values[1].code,
            town: e.detail.values[2].code,
            show: false
        })
        if (e.detail.values[0].name === e.detail.values[1].name) {
            this.setData({
                area: e.detail.values[0].name
            })
        } else {
            this.setData({
                area: e.detail.values[0].name + " " + e.detail.values[1].name + " " + e.detail.values[2].name,

            })
        }
    },
    showVan() {
        this.setData({
            show: true
        })
    },
    add() {
        let token = wx.getStorageSync('token')
        let that = this
        console.log(that.data)
        let pattern = /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/
        if (that.data.province === "") {
            wx.showToast({
                title: "请填写地址!",
                duration: 1000,
                icon: "none"
            })
        } else if (that.data.name === "") {
            wx.showToast({
                title: "请输入姓名!",
                duration: 1000,
                icon: "none"
            })
        } else if (!pattern.test(that.data.phoneNum)) {
            wx.showToast({
                title: "请输入正确的手机号!",
                duration: 1000,
                icon: "none"
            })
        } else {
            let phone = parseInt(that.data.phoneNum)
            let formdata = '\r\n--XXX' +
                '\r\nContent-Disposition: form-data; name="province"' +
                '\r\n' +
                '\r\n' + that.data.province +
                '\r\n--XXX' +
                '\r\nContent-Disposition: form-data; name="city"' +
                '\r\n' +
                '\r\n' + that.data.city +
                '\r\n--XXX' +
                '\r\nContent-Disposition: form-data; name="detailAddress"' +
                '\r\n' +
                '\r\n' + that.data.detailAddress +
                '\r\n--XXX' +
                '\r\nContent-Disposition: form-data; name="name"' +
                '\r\n' +
                '\r\n' + that.data.name +
                '\r\n--XXX' +
                '\r\nContent-Disposition: form-data; name="phoneNum"' +
                '\r\n' +
                '\r\n' + phone +
                '\r\n--XXX' +
                '\r\nContent-Disposition: form-data; name="town"' +
                '\r\n' +
                '\r\n' + that.data.town +
                '\r\n--XXX--'
            wx.request({

                url: "https://www.cbing.icu:8095/wxUser/addAddress",
                method: 'POST',
                header: {
                    'content-type': 'multipart/form-data;boundary=XXX',
                    'Authorization': token
                },
                data: formdata,
                success(res) {
                    if (res.data.code === 200) {

                        wx.navigateBack({
                            delta: 1,  // 返回上一级页面。
                            success(res) {
                                wx.showToast({
                                    title: "添加成功！",
                                    icon: "none",
                                    duration: 1000
                                })
                            }
                        })

                    } else {
                        wx.showToast({
                            title: "添加失败！",
                            icon: "none",
                            duration: 1000
                        })
                    }
                }
            })
        }

    }

})